package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// R Response ServerResult Result

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

var (
	CODE       = 20000
	ERROR_CODE = 40001
	MSG        = "success"
)

func Result(code int, msg string, data any, c *gin.Context) {
	c.JSON(http.StatusOK, Response{code, msg, data})
}

func Ok(data interface{}, c *gin.Context) {
	Result(CODE, MSG, data, c)
}

// Fail struct{}{}占位符  因为result 必须要有4个参数
func Fail(code int, msg string, c *gin.Context) {
	Result(code, msg, struct{}{}, c)
}

func FailWithMessage(msg string, c *gin.Context) {
	Result(ERROR_CODE, msg, map[string]any{}, c)
}
func FailWithData(code int, msg string, data any, c *gin.Context) {
	Result(code, msg, data, c)
}
